/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author ikmlop
 */
class DialogConnectToServer extends JDialog implements ActionListener
{

    private JTextField txtServerPath;
    private JTextField txtUser;
    private JTextField txtPass;
    private Container contentPane;
    private SpringLayout springLayout = new SpringLayout();
    private frmMain _frmParent;

    DialogConnectToServer(frmMain frmParent)
    {
        super(frmParent, "", true);
        _frmParent = frmParent;
        InitComponents();


        setVisible(false);
        setSize(380, 280);
        setResizable(false);
        setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void InitComponents()
    {
        String connectToServerStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "connecttoserver");
        this.setTitle(connectToServerStr);
        
        setLayout(springLayout);
        contentPane = this.getContentPane();
        contentPane.removeAll();;

        add(createButton("Select", "...", 330, 35, 20, 20));

        String connectStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "connect");
        String cancelStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "cancel");
        add(createButton("Ok", connectStr, 70, 175, 100, 30));
        add(createButton("Cancel", cancelStr, 190, 175, 100, 30));

        String serverPathStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "serverpath");
        String userNameStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "username");
        String passwordStr = MultilanguageHelper.GetResource(_frmParent.getCurrentLocale(), "password");

        add(createLabel(serverPathStr, 20, 35));
        add(createLabel(userNameStr, 20, 75));
        add(createLabel(passwordStr, 20, 115));

        txtServerPath = createTextField(100, 35, 20);
        txtServerPath.setEditable(false);
        txtUser = createTextField(100, 75, 20);
        txtPass = createTextField(100, 115, 20);

        add(txtServerPath);
        add(txtUser);
        add(txtPass);
    }

    protected JButton createButton(String actionCommandButton, String altTextButton, int x, int y, int width, int height)
    {
        JButton btn = new JButton(altTextButton);
        btn.setPreferredSize(new Dimension(width, height));
        btn.setActionCommand(actionCommandButton);
        btn.addActionListener(this);
        springLayout.putConstraint(SpringLayout.WEST, btn, x, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, btn, y, SpringLayout.NORTH, contentPane);
        return btn;
    }

    protected JLabel createLabel(String altText, int x, int y)
    {
        JLabel label = new JLabel(altText);
        springLayout.putConstraint(SpringLayout.WEST, label, x, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, label, y, SpringLayout.NORTH, contentPane);
        return label;
    }

    private JTextField createTextField(int i, int i0, int wordCound)
    {
        JTextField txt = new JTextField(wordCound);
        springLayout.putConstraint(SpringLayout.WEST, txt, i, SpringLayout.WEST, contentPane);
        springLayout.putConstraint(SpringLayout.NORTH, txt, i0, SpringLayout.NORTH, contentPane);
        return txt;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if (actionCommand.equalsIgnoreCase("Ok"))
        {

            //aThis.getInformationFromDialog(txtServerPath.getText().toString(), txtUser.getText().toString(), txtPass.getText().toString());
            setVisible(false);

        }
        else if (actionCommand.equalsIgnoreCase("Select"))
        {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fc.setAcceptAllFileFilterUsed(true);
            if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            {
                txtServerPath.setText(fc.getSelectedFile().toString());
            }
            else
            {
                txtServerPath.setText(null);
            }
        }
    }
}
